//
//  WYHUD.m
//  test
//
//  Created by longtu on 15/5/8.
//  Copyright (c) 2015年 longtu. All rights reserved.
//

#import "WYDevHelper.h"

@interface WYProgressHUD()
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UILabel *lbl_alert;
@property (nonatomic, strong) UILabel *lbl_show;
@property (nonatomic, strong) UIView *viewActivityBg;
@property (nonatomic, strong) UIActivityIndicatorView *acInd;

- (void) show:(NSString *)string hideTime:(float)hideTime;
- (void) showLoadingView;
- (void) hiddenLoadingView;
@end

@implementation WYProgressHUD
- (id)init{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate respondsToSelector:@selector(window)])
        _window = [delegate performSelector:@selector(window)];
    else _window = [[UIApplication sharedApplication] keyWindow];
    self.alpha = 0;
    return self;
}

+ (WYProgressHUD *)shared{
    static dispatch_once_t once = 0;
    static WYProgressHUD *progressHUD;
    dispatch_once(&once, ^{ progressHUD = [[WYProgressHUD alloc] init]; });
    return progressHUD;
}

// 显示加载中
+ (void) showLoading{
    [[self shared] showLoadingView];
}

// 移除加载提示
+ (void) hiddenLoading{
    [[self shared] hiddenLoadingView];
}

// 显示提示文字
+ (void) showMessage:(NSString *)string hideTime:(float)hideTime{
    [[self shared] show:string hideTime:hideTime];
}

- (void) show:(NSString *)string hideTime:(float)hideTime{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    if (self.lbl_alert == nil) {
        self.lbl_alert = [[UILabel alloc] init];
        //设置字号
        self.lbl_alert.font = [UIFont systemFontOfSize:14];
        //位置
        self.lbl_alert.frame = CGRectMake((size.width-(string.length*20+20))/2, size.height-(size.height/4), string.length*20+20, 30);
        //赋值
        self.lbl_alert.text = string;
        //字体居中
        self.lbl_alert.textAlignment = NSTextAlignmentCenter;
        //字体颜色
        self.lbl_alert.textColor = [UIColor whiteColor];
        //设置背景色
        self.lbl_alert.backgroundColor = [UIColor blackColor];
        //设置背景透明
        self.lbl_alert.alpha = 0.5;
        //设置背景为圆角矩形
        self.lbl_alert.layer.cornerRadius = 5;
        self.lbl_alert.layer.masksToBounds = YES;
    }
    if (self.lbl_alert.superview == nil) [self.window addSubview:self.lbl_alert];
    //定时3s之后，自动停止运行
    [NSTimer scheduledTimerWithTimeInterval:hideTime target:self selector:@selector(timerRemove:) userInfo:self.lbl_alert repeats:NO];
}
//移除
-(void)timerRemove:(NSTimer *)timer{
    UILabel *lable_lbl = timer.userInfo;
    [lable_lbl removeFromSuperview];
}

-(void)showLoadingView{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    /* 进度指示器背景 */
    if (self.viewActivityBg == nil){
        self.viewActivityBg = [[UIView alloc] initWithFrame:CGRectMake(size.width/2 - 50 , size.height/2 - 50 , 100, 100)];
        //设置那个圆角的有多圆
        self.viewActivityBg.layer.cornerRadius = 10;
        //设为NO去试试
        self.viewActivityBg.layer.masksToBounds = NO;
        self.viewActivityBg.backgroundColor = RGBA(50, 50, 50, 1);
        self.viewActivityBg.alpha = 0.5;
        self.viewActivityBg.tag = 106;
        self.window.userInteractionEnabled = NO;
    }
    if (self.viewActivityBg.superview == nil) [self.window addSubview:self.viewActivityBg];
    
    /* 进度指示器 */
    if (self.acInd == nil){
        self.acInd = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, -5, 100, 100)];
        self.acInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        self.acInd.color = [UIColor whiteColor];
        //停止后是否隐藏(默认为YES)
        self.acInd.hidesWhenStopped = YES;
        [self.acInd startAnimating];
    }
    if (self.acInd.superview == nil) [self.viewActivityBg addSubview:self.acInd];
    
    /* 显示loading.. */
    if (self.lbl_show == nil){
        self.lbl_show = [[UILabel alloc] initWithFrame:CGRectMake(0, self.acInd.frame.size.height - 25, self.viewActivityBg.frame.size.width, self.viewActivityBg.frame.size.height-self.acInd.frame.size.height + 20)];
        self.lbl_show .backgroundColor = [UIColor clearColor];
        self.lbl_show .text = @"Loading..";
        self.lbl_show .textColor = [UIColor whiteColor];
        self.lbl_show .textAlignment = NSTextAlignmentCenter;
    }
    if (self.lbl_show.superview == nil) [self.viewActivityBg addSubview:self.lbl_show ];
}

-(void)hiddenLoadingView{
    [self.acInd stopAnimating];
    [self.viewActivityBg.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.window.userInteractionEnabled = YES;
    [self.viewActivityBg removeFromSuperview];
}

@end
